package com.yanboot.iot.utils.gb26875.utils;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Hsm256 {
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        String hmacSha256 = createHmacSha256("0104681ec9d54a73bd1afd664244477e", "1734939374576");
        System.out.println("hmacSha256 = " + hmacSha256);
    }


    public static String createHmacSha256(String secret, String message) {
        String res;
        try {
            Mac mac = Mac.getInstance("HmacSHA256");
            SecretKey secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            mac.init(secretKey);
            byte[] hash = mac.doFinal(message.getBytes());
            res = Hex.encodeHexString(hash);
        } catch (Exception e) {
            return null;
        }
        return res;
    }
}
